package com.bawei.module_motocircle.ui.post.detail
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager

import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router

import com.bawei.lib_common.base.BaseToolBarActivity
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.state.UILoader
import com.bawei.lib_common.base.state.UIState
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_motocircle.R
import com.bawei.module_motocircle.databinding.ActivityDetailsBaseBinding
import com.bawei.module_motocircle.databinding.LayoutPostDetailsContentBinding
import com.bawei.module_motocircle.ui.post.detail.adapter.PostDetailAdapter
import dagger.hilt.android.AndroidEntryPoint
/**
 * 帖子详情
 */
@AndroidEntryPoint
@Route(path = Router.Ui.PostDetailsActivity)
class PostDetailActivity : BaseToolBarActivity<PostDetailViewModel,ActivityDetailsBaseBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_details_base
    val mViewModel: PostDetailViewModel by viewModels()

    val mPostDetailAdapter:PostDetailAdapter by lazy {
        PostDetailAdapter()
    }


    override fun initView() {
        super.initView()
        initUILoader()
        initAdapter()
    }

    override fun initToolbar(toolbar: Toolbar) {
        mBinding.appbar.toolbarTitle.text = getString(R.string.string_post_details)
        mToolbar.inflateMenu(R.menu.single_menu)
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action) {
                if (!AuthMMKV.hasLogin()) {
                    return@setOnMenuItemClickListener true
                }

                // clickMoreAction()
//                clickMoreAction2()
            }
            true
        }
    }

    private fun initAdapter() {
        mContentBinding.rvPostDetails.adapter = mPostDetailAdapter
        mContentBinding.rvPostDetails.layoutManager = LinearLayoutManager(this)
    }

    private lateinit var mUiLoader: UILoader
    private lateinit var mContentBinding: LayoutPostDetailsContentBinding
    private fun initUILoader() {
        mContentBinding = DataBindingUtil.inflate(
            LayoutInflater.from(this),
            R.layout.layout_post_details_content,
            null,
            false
        )
        mUiLoader = object : UILoader(this) {
            override fun loadSuccessView(container: ViewGroup?): View {
                return mContentBinding.root
            }
        }.apply { updateState(UIState.LOADING) }

        mBinding.layoutContent.addView(mUiLoader)
    }


    override fun initData() {
        mViewModel.getPostDetailData()
    }

    override fun initListener() {
        mViewModel.mPostState.observeWithLifecycle(this){
            when(it){
                is Loading -> {
                    mUiLoader.updateState(UIState.LOADING)

                }
                is Error ->{
                    mUiLoader.updateState(UIState.NETWORK_ERROR)
                }
                is Success -> {
                    mUiLoader.updateState(UIState.SUCCESS)
                    //数据
                    mPostDetailAdapter.setPostItem(mViewModel.currentPost!!)
                    mPostDetailAdapter.setAfterSale(mViewModel.afterSale!!)
                    mPostDetailAdapter.setComments(mViewModel.comments!!)


                }
                else -> {

                }
            }
        }
    }

}